package com.question.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.question.Dto.PaperAnswerDto;
import com.question.Dto.QAnswer;
import com.question.Exception.BaseException;
import com.question.common.PageResult;
import com.question.entity.Paper;
import com.question.entity.PaperUser;
import com.question.entity.Records;
import com.question.mapper.PaperMapper;
import com.question.mapper.PaperUserMapper;
import com.question.mapper.RecordsMapper;
import com.question.service.PaperService;
import com.question.service.PaperUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PaperUserServiceImpl extends ServiceImpl<PaperUserMapper, PaperUser> implements PaperUserService {
    @Resource
    private PaperUserMapper paperUserMapper;
    @Resource
    private PaperMapper paperMapper;
    @Resource
    private RecordsMapper recordsMapper;

    @Transactional(rollbackFor = Exception.class)
    public void adds(Integer pid, Integer uid) {
        LambdaQueryWrapper<PaperUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PaperUser::getPid, pid);
        lambdaQueryWrapper.eq(PaperUser::getUid, uid);
        PaperUser paperUser = paperUserMapper.selectOne(lambdaQueryWrapper);
        if (paperUser != null) {
            throw new BaseException("该问卷已发放给该用户");
        }

        PaperUser paperUser1 = new PaperUser();
        paperUser1.setCreatetime(new Date());
        paperUser1.setStatus(0);
        paperUser1.setPid(pid);
        paperUser1.setUid(uid);
        paperUserMapper.insert(paperUser1);
        Paper paper = paperMapper.selectById(pid);
        paperMapper.updateUserCount(paper.getId(),1);

    }

    public PageResult getpage(Integer pageNo, Integer pageSize) {
        IPage<PaperUser> page = new Page<>(pageNo, pageSize);
        IPage<PaperUser> page1 = paperUserMapper.selectBypage(page);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(page1.getRecords());
        pageResult.setTotal(page1.getTotal());
        return pageResult;
    }
    public List<Paper> getByUserId(Integer id){
        List<Paper> list=paperMapper.getByUserId(id);
        return list;
    }
    public List<Paper> getfinishByUserId(Integer id){
        List<Paper> list=paperMapper.getfinishByUserId(id);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public     void submit(Integer id, PaperAnswerDto paperAnswerDto){
        LambdaQueryWrapper<Records> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Records::getUid,id);
        lambdaQueryWrapper.eq(Records::getPid,paperAnswerDto.getPid());
        Records records=recordsMapper.selectOne(lambdaQueryWrapper);
        if(records !=null){
            throw new BaseException("你已答完该问卷");
        }

        for (QAnswer qAnswer: paperAnswerDto.getList()){
            Records records1=new Records();
            records1.setCreatetime(new Date());
            records1.setAnswer(qAnswer.getAnswer());
            records1.setPid(paperAnswerDto.getPid());
            records1.setUid(id);
            records1.setQid(qAnswer.getQid());
            recordsMapper.insert(records1);
        }
        LambdaQueryWrapper<PaperUser> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(PaperUser::getPid,paperAnswerDto.getPid());
        lambdaQueryWrapper1.eq(PaperUser::getUid,id);
        PaperUser paperUser=paperUserMapper.selectOne(lambdaQueryWrapper1);
        paperUser.setStatus(1);
        paperUserMapper.updateById(paperUser);

    }

}
